package com.ews.onlineexchange.rest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.ews.onlineexchange.app.CustomMapper;
import com.ews.onlineexchange.app.NotificationListForApp;
import com.ews.onlineexchange.app.entity.NotificationApp;
import com.ews.onlineexchange.app.entity.TempNotification;
import com.ews.onlineexchange.model.Notification;
import com.ews.onlineexchange.service.NotificationService;
import com.fasterxml.jackson.core.JsonProcessingException;

import net.sf.json.JSONObject;


/**
 * 消息通知接口
 * @author
 *
 */
@Path(value = "/notification")
@Transactional
public class NotifictionRest {
	@Inject
	EntityManager entityManager;

	@GET
	@Path("/setNotificationRead/{noId}")
	@Produces("text/html; charset=UTF-8")
	@Context
	public void updateNotificationRead(@PathParam("noId") String noId) {
		Notification no = entityManager.find(Notification.class, noId);
		if(!no.isHasRead()) {
			no.setHasRead(true);
			no.setReadDate( new Date());
			entityManager.merge(no);
		}
		
	}
	
	@Inject
	NotificationListForApp notificationListForApp;
	@GET
	@Path("/getNotificationList/{userId}/{pageNumber}/{pageSize}")
	@Produces("text/html; charset=UTF-8")
	@Context
	public String getNotificationList(@PathParam("userId") String userId,@PathParam("pageNumber") int pageNumber,@PathParam("pageSize") int pageSize) throws JsonProcessingException {
		notificationListForApp.setUserId(userId);
		notificationListForApp.refresh();
		notificationListForApp.setFirstResult((pageNumber-1) * pageSize);
		notificationListForApp.setMaxResults(pageSize);
		List<Notification> nos = notificationListForApp.getResultList();
		Long totalcount = notificationListForApp.getResultCount();
		CustomMapper mapper = new CustomMapper();
		List<TempNotification> tempNos = new ArrayList<>();
		for (Notification notification : nos) {
			TempNotification tempNo = new TempNotification();
			tempNo.setId(notification.getId());
			tempNo.setContent(notification.getContent());
			tempNo.setTitile(notification.getTitile());
			tempNo.setSendDate(notification.getSendDate());
			tempNo.setHasRead(notification.isHasRead());
			if(notification.getBulletin() == null) {
				tempNo.setBulletinId("");
			}else {
				tempNo.setBulletinId(notification.getBulletin().getId());
			}
			tempNos.add(tempNo);
		}
	  JSONObject jsonnObject = new JSONObject();
      jsonnObject.put("totalcount", totalcount);
      jsonnObject.put("notificationArr", tempNos);
      return mapper.writeValueAsString(jsonnObject);
		
	}
	
	
	@GET
	@Path("/getNotificationById/{noId}")
	@Produces("application/json")
	@Context
	public NotificationApp getNotificationById(@PathParam("noId") String noId) throws JsonProcessingException {
		NotificationApp noApp = new NotificationApp();
		Notification no = entityManager.find(Notification.class, noId);
		noApp.setId(no.getId());
		noApp.setContent(no.getContent());
		noApp.setHasRead(no.isHasRead());
		noApp.setReadDate(no.getReadDate());
		noApp.setSendDate(no.getReadDate());
		noApp.setTitile(no.getTitile());
		return noApp;
		
	}
	@Inject
	NotificationService notificationService;
	
	@GET
	@Path("/getNotReadNotificationByUserId/{userId}")
	@Produces("application/json")
	@Context
	public long getNotReadNotificationByUserId(@PathParam("userId") String userId) throws JsonProcessingException {
		return notificationService.getNotReadNotificationCountByUserId(userId);
		
	}	
	
}
